<?php

namespace App\Exceptions;

use App\Traits\ReponseJson;
use Exception;
use Illuminate\Validation\Validator;

/**
 * Class ValidationException
 * @package App\Exceptions
 */
class ValidationException extends Exception
{
    /**
     * 所以验证器相关的验证异常
     */
    use ReponseJson;

    public function __construct(Validator $validator, $message = '', $code = 0)
    {
        $this->validator = $validator;
        $this->code      = $code;
        $this->message   = $message ?: $this->validator->errors()->all()[0];
    }

    public function render()
    {
        info($this->message);
        return $this->failure(
            $this->message,
            $this->validator->errors()->all(),
            $this->code
        );
    }
}
